package sufutian.com.frameworkdemo.network;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;

public class Json implements IJson {
    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    String requestSourceName;
    int requestBusinessType = -1;
    JSONObject root = null;

    public Json(String jsonString) {
        try {
            root = new JSONObject(jsonString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Json(JSONObject jsonObject) {
        root = jsonObject;
    }

    public Json() {
        root = new JSONObject();
    }
    public JSONObject getRoot(){
        return root;
    }
    public boolean put(String key, int value) {
        try {
            root.put(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean put(String key, Object value) {
        try {
            root.put(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String toString() {
        if (root == null) {
            return null;
        }
        return root.toString();
    }
    
    @Override
    public boolean isNull(String name) {
    	 try {
    		 return root.isNull(name);
    	 }catch(Exception e){
    		 return true;
    	 }
    }
    
    public String getString(String key) {
        try {
            return root.get(key).toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public int getInt(String key) {
        try {
            return root.getInt(key);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public boolean has(String key) {
        try {
            return root.has(key);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public Iterator<String> keys() {
        return (Iterator<String>) root.keys();
    }

    public String firstKey() {
        return keys().next();
    }

    public boolean getBoolean(String key) {
        try {
            return root.getBoolean(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Json getJson(String key) {
        try {
            return new Json(root.getJSONObject(key));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Json[] getJsonArray(String key) {
        Json[] jsons;
        try {
            JSONArray a = root.getJSONArray(key);
            jsons = new Json[a.length()];
            for (int i = 0; i < jsons.length; i++) {
                jsons[i] = new Json(a.getJSONObject(i));
            }

            return jsons;
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }
    }
    
    public String[] getArray(String key) {
    	String[] strs;
        try {
            JSONArray a = root.getJSONArray(key);
            strs = new String[a.length()];
            for (int i = 0; i < strs.length; i++) {
                strs[i] = a.getString(i);
            }
            return strs;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void setRequestSourceName(String interfaceName) {
        this.requestSourceName = interfaceName;
    }

    @Override
    public void setRequestBusinessType(int businessType) {
        this.requestBusinessType = businessType;
    }

    @Override
    public String getRequestSourceName() {
        return requestSourceName;
    }

    @Override
    public int getRequestBusinessType() {
        return requestBusinessType;
    }

}
